package com.tensquare.search.controller;

import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleSearchService;
import entity.PageResult;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import util.ResultUtil;

@RestController
@RequestMapping("/article")
public class ArticleSearchController {

    @Autowired
    private ArticleSearchService articleSearchService;

    /**
     * 新增文章ss
     *
     * @param article
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Article article) {
        articleSearchService.add(article);
        return ResultUtil.success();
    }

    @GetMapping("/search/{keywords}/{page}/{size}")
    public Result findByTitleLike(@PathVariable String keywords, @PathVariable int page,
                                  @PathVariable int size) {
        Page<Article> titleLike = articleSearchService.findByTitleLike(keywords, page, size);
        return ResultUtil.success(
                new PageResult<Article>(titleLike.getTotalElements(), titleLike.getContent()));
    }

}
